<template>
  <el-form
    class="registe-container"
    :rules="rules"
    :model="registeForm"
    ref="registeForm"
  >
    <h3 class="registe-title">注册账号</h3>
    <el-form-item prop="username">
      <el-input type="text" label="账号" v-model="registeForm.username"></el-input>
    </el-form-item>
    <el-form-item prop="password">
      <el-input type="password" label="密码" v-model="registeForm.password"></el-input>
    </el-form-item>
    <el-form-item style="width: 100%">
      <el-button type="primary" @click="submitForm('registeForm')">确定</el-button>
      <el-button type="primary" @click="resetForm('registeForm')">重置</el-button>
      <el-button type="primary" @click="returnLogin()">返回</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { postRequestByParams } from "../utils/api";

export default {
  data() {
    return {
      registeForm: {
        username: "",
        password: ""
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 8, max: 20, message: "用户名长度不正确", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 1, max: 20, message: "密码长度不正确", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.loading = true;
          var _this = this;
          postRequestByParams("/account/add-common-account", {
            username: _this.registeForm.username,
            password: _this.registeForm.password
          }).then(resp => {
            _this.loading = false;
            if (resp.status == 200) {
              var json = resp.data;
              this.$alert(json.data);
              if (json.message == "success") {
                this.$router.replace({ path: "/" });
              }
            } else {
              this.$alert("未知错误！");
            }
            _this.resetForm("registeForm");
          });
        } else {
          this.$alert("输入错误！");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    returnLogin: function() {
      this.$router.replace({ path: "/" });
    }
  }
};
</script>

<style>
.registe-container {
  border-radius: 30px;
  margin: 180px auto;
  width: 350px;
  padding: 35px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
}

.registe-title {
  margin: 0px auto 40px auto;
}
</style>